package ugroupinterceptor

import (
	"context"
	"encoding/json"
	"github.com/go-playground/validator"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/model/adminmodel"
	"vpn-management-server/src/model/ugroupmodel"
	"vpn-management-server/src/service/adminservice"
)

func UGroupValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroup ugroupmodel.UGroup
	err := json.Unmarshal(request.Parameters, &UGroup)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroup error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(&UGroup)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SystemAdmin || rank == adminmodel.SuperAdmin {
		request.SetModel(&UGroup)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func UGroupNameInfoValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupNameInfo ugroupmodel.UGroupNameInfo
	err := json.Unmarshal(request.Parameters, &UGroupNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroupNameInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(&UGroupNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SystemAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&UGroupNameInfo)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func PageValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var page ugroupmodel.Page
	err := json.Unmarshal(request.Parameters, &page)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to page error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(&page)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SystemAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&page)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func PageWithIDValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var PageWithID ugroupmodel.PageWithID
	err := json.Unmarshal(request.Parameters, &PageWithID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to page error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(&PageWithID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SystemAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&PageWithID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func AdminRankValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SystemAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin || rank == adminmodel.AuditAdmin {
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetUserGroupByNameValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupNameInfo ugroupmodel.UGroupNameInfo
	err := json.Unmarshal(request.Parameters, &UGroupNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroupNameInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	// TODO 验证json字段的合法性
	valid := validator.New()
	err = valid.Struct(&UGroupNameInfo)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限等级
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SystemAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&UGroupNameInfo)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}
